<?php

class DefaultController extends MyUserController
{
	public $modelName = 'Files';
	
	
//	public function actionUpload()
//	{
//		$formModel = new UploadForm;
//
//		// if it is ajax validation request
//		if(isset($_POST['ajax']) && $_POST['ajax']==='upload-form')
//		{
//			$formModel->filename = CUploadedFile::getInstance($formModel, 'filename');
//			echo CActiveForm::validate($formModel);
//			Yii::app()->end();
//		}
//		
//		if(isset($_POST['UploadForm']))
//		{
//			$formModel->attributes = $_POST['UploadForm'];
//			$formModel->filename = CUploadedFile::getInstance($formModel, 'filename');
//			/* check upload limit */
//			
//			// validate user input and redirect to the previous page if valid
//			if($formModel->validate())
//			{
//				$model = new Files();
//				$model->prepareFile($formModel);
//				if ($model->save(false))
//				{
//					Yii::app()->user->setFlash('success', tt('File uploaded.'));
//				}
//				else
//				{
//					$errors = print_r($model->getErrors(), true);
//					Yii::app()->user->setFlash('error', $errors);
//				}
//			}
//			else
//			{
//				$errors = print_r($formModel->getErrors(), true);
//				Yii::app()->user->setFlash('error', $errors);
//			}
//			$this->redirect(Yii::app()->homeUrl);
//		}
//	}

	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			Yii::app()->user->setFlash('success', tt('File successfully deleted'));
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : Yii::app()->homeUrl);
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
}